/*
 * File: UserTest.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package faceBook.tests;

import static org.junit.Assert.*;

import java.sql.Date;

import org.junit.Test;

import tests.AppEngineTest;

import faceBook.User;
import faceBook.tests.helper.TestUsers;

/**
 * The Class UserTest.
 */
@SuppressWarnings("static-method")
public class UserTest extends AppEngineTest
{

  /**
   * Tests the getID method.
   */
  @Test
  public void testGetID()
  {
    User user = new User(4);
    assertEquals(4, user.getID());
  }

  /**
   * Tests the getName method.
   */
  @Test
  public void testGetAccessToken()
  {
    User user = new User(123);
    user.setAccessToken("abc");
    assertEquals("abc", user.getAccessToken());
  }

  /**
   * Test get birthday.
   */
  @Test
  public void testGetBirthday()
  {
    User user = new User(123);
    user.setBirthday(new Date(350000000000L));
    assertEquals(new Date(350000000000L).toString(), user.getBirthday()
        .toString());
  }

  /**
   * Test get expire time.
   */
  @Test
  public void testGetExpireTime()
  {
    User user = new User(123);
    user.setExpireTime(new Date(450000000000L));
    assertEquals(new Date(450000000000L).toString(), user.getExpireTime()
        .toString());
  }

  /**
   * Test get email.
   */
  @Test
  public void testGetEmail()
  {
    User user = new User(123);
    user.setEmail("dude@dude.com");
    assertEquals("dude@dude.com", user.getEmail());
  }

  /**
   * Tests the getReply method.
   */
  @Test
  public void testGetReply()
  {
    User user = new User(123);
    user.setReplyMessage("my fake reply");
    assertEquals("my fake reply", user.getReplyMessage());
  }

  /**
   * Tests the getReply method.
   */
  @Test
  public void testGetDefaultBDMessageMale()
  {
    User user = new User(123);
    user.setDefaultBDMessageMale("my fake BD Message");
    assertEquals("my fake BD Message", user.getDefaultBDMessageMale());
  }

  /**
   * Tests the getReply method.
   */
  @Test
  public void testGetDefaultBDMessageFemale()
  {
    User user = new User(123);
    user.setDefaultBDMessageFemale("my fake BD Message");
    assertEquals("my fake BD Message", user.getDefaultBDMessageFemale());
  }

  /**
   * Tests the getReply method.
   */
  @Test
  public void testGetUseDefaultBD()
  {
    User user = new User(123);
    user.setUseDefaultBD(true);
    assertTrue(user.getUseDefaultBD());
  }

  /**
   * Tests the getReply method.
   */
  @Test
  public void testGetUseReply()
  {
    User user = new User(123);
    user.setUseReply(true);
    assertTrue(user.getUseReply());
  }

  /**
   * Tests the dataBase method.
   */
  @Test
  public void testDataBase()
  {
    User user = new User(123);
    user.setAccessToken("testAC");
    user.setBirthday(new Date(350000000000L));
    user.setEmail("dude@dude.com");
    user.setExpireTime(new Date(150000000000L));
    user.setReplyMessage("my fake reply");
    user.setDefaultBDMessageMale("my fake male BD Message");
    user.setDefaultBDMessageFemale("my fake female BD Message");
    user.setUseDefaultBD(true);
    user.setUseReply(true);
    user.setGender("male");
    user.setFirstName("Bob");
    user.setLastName("Butts");
    user.setFan(true);
    assertTrue(user.save());// Save the object
    User user2 = new User(user.getID());// Load the object
    assertEquals("testAC", user2.getAccessToken());
    assertEquals(new Date(350000000000L).toString(), user2.getBirthday()
        .toString());
    assertEquals(new Date(150000000000L).toString(), user2.getExpireTime()
        .toString());
    assertEquals("dude@dude.com", user2.getEmail());
    assertEquals("my fake reply", user2.getReplyMessage());
    assertEquals("my fake male BD Message", user2.getDefaultBDMessageMale());
    assertEquals("my fake female BD Message", user2.getDefaultBDMessageFemale());
    assertTrue(user2.getUseDefaultBD());
    assertTrue(user2.getUseReply());
    assertEquals("M", user2.getGender());
    assertEquals("Bob", user2.getFirstName());
    assertEquals("Butts", user2.getLastName());
    assertTrue(user2.isFan());
    user2.setAccessToken("newtestAC");
    user2.setBirthday(new Date(550000000000L));
    user2.setEmail("otherdude@dude.com");
    user2.setExpireTime(new Date(350000000000L));
    user2.setReplyMessage("my new fake reply");
    user2.setDefaultBDMessageMale("my new fake male BD Message");
    user2.setDefaultBDMessageFemale("my new fake female BD Message");
    user2.setUseDefaultBD(false);
    user2.setUseReply(false);
    user2.setGender("female");
    user2.setFirstName("John");
    user2.setLastName("Smith");
    user2.setFan(false);
    assertTrue(user2.save());// Save the object
    User user3 = new User(user.getID());// Load the object again
    assertEquals("newtestAC", user3.getAccessToken());
    assertEquals(new Date(550000000000L).toString(), user3.getBirthday()
        .toString());
    assertEquals(new Date(350000000000L).toString(), user3.getExpireTime()
        .toString());
    assertEquals("otherdude@dude.com", user3.getEmail());
    assertEquals("my new fake reply", user3.getReplyMessage());
    assertEquals("my new fake male BD Message", user3.getDefaultBDMessageMale());
    assertEquals("my new fake female BD Message", user3.getDefaultBDMessageFemale());
    assertFalse(user3.getUseDefaultBD());
    assertFalse(user3.getUseReply());
    assertEquals("F", user3.getGender());
    assertEquals("John", user3.getFirstName());
    assertEquals("Smith", user3.getLastName());
    assertFalse(user3.isFan());
    user.delete();
    User user4 = new User(user.getID());
    assertEquals(null, user4.getAccessToken());
    assertEquals(null, user4.getBirthday());
    assertEquals(null, user4.getExpireTime());
    assertEquals(null, user4.getEmail());
    assertEquals(null, user4.getReplyMessage());
    assertEquals(null, user4.getDefaultBDMessageMale());
    assertEquals(null, user4.getDefaultBDMessageFemale());
    assertFalse(user4.getUseDefaultBD());
    assertEquals(null, user4.getGender());
    assertEquals(null, user4.getFirstName());
    assertEquals(null, user4.getLastName());
    assertFalse(user4.isFan());
  }

  /**
   * Tests the toJSONString method.
   */
  @Test
  public void testToJSONString()
  {
    User user = new User(123);
    user.setAccessToken("testName");
    user.setBirthday(new Date(350000000000L));
    user.setEmail("dude@dude.com");
    user.setExpireTime(new Date(150000000000L));
    user.setReplyMessage("my replyMessage");
    user.setDefaultBDMessageMale("my male BDMessage");
    user.setDefaultBDMessageFemale("my female BDMessage");
    user.setUseDefaultBD(true);
    user.setUseReply(true);
    assertEquals(
        "{\"defaultBDMessageFemale\":\"my female BDMessage\",\"expireTime\":\"1974-10-03\",\"id\":123,\"birthday\":\"1981-02-02\",\"defaultBDMessageMale\":\"my male BDMessage\",\"accessToken\":\"testName\",\"email\":\"dude@dude.com\",\"useDefaultDB\":true,\"useReply\":true,\"replyMessage\":\"my replyMessage\"}",
        user.toJSONString());
  }

  /**
   * Tests the getAllActiveUsersAccessTokens method.
   */
  @Test
  public void testGetAllActiveUsers()
  {
    User user = new User(123);
    user.setAccessToken("abc");
    user.save();
    assertTrue(User.getAllActiveUsers().toString().contains("\"accessToken\":\"abc\","));
    user.delete();
  }
  
  /**
   * Tests the postPermissions method.
   */
  @Test
  public void testPostPermissions()
  {
    User testUser = TestUsers.getTestUser(0);
    assertTrue(testUser.hasPostPermissions());
  }

}
